<?php
/**
 * 飞信机器人--功能插件--基类
 * 功能：所有插件，可以继承该类
 * 数据表：无
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
class CommonPlugin
{
	var $session;
	var $pid;
	var $extraParam;
	var $admin=array();
	var $UUID;
	var $depth=1;
	
	function __construct(&$session, $pid, $extraPara="")
	{
		$this->session = &$session;
		$this->pid = $pid;
		$this->getDepth($pid);
		parse_str($extraPara, $this->extraParam);
		$this->admin = explode(",",isset($this->extraParam['admin'])?$this->extraParam['admin']:'');
	}
	private function getDepth($pid)
	{
		if($pid==0)return;
		while(1)
		{
			$sql = "select p_id from fx_menus where id=$pid";
			$rows = $this->query($sql);
			if($rows[0]['p_id'] != 0 )
			{
				$this->depth++;
				$pid = $rows[0]['p_id'];
			}else
			{
				$this->depth++;
				return;
			}
		}
	}
	function CommonPlugin(&$session, $pid, $extraPara="")
	{
		$this->session = &$session;
		$this->pid = $pid;
		parse_str($extraPara, $this->extraParam);
	}
	
	public function isAdmin()
	{
		for($i=0; $i<count($this->admin); $i++)
		{
			if($this->admin[$i] == $this->session->buddyArray['sid'])return true;
		}
		return false;
	}
	
	function getFooter($extra="",$shortcut = false)
	{
		if( $shortcut ) return "";
		
		if( $this->depth>1 )
			return "\n<Font Face='楷体' Color='-8388608' Size='10.5'>【m-返回主菜单,q-返回上一级{$extra}】</Font>";
		else 
			return "\n<Font Face='楷体' Color='-8388608' Size='10.5'>【m-返回主菜单{$extra}】</Font>";
	}
	
	function backMain()
	{
		$this->session->set('NODE',0);
		$this->session->set('PLUGIN','');
	}
	function backUp()
	{
		$this->session->set('NODE',$this->pid);
		$this->session->set('PLUGIN','');		
	}
	
	function handleInput($input,$shortcut = false)
	{
		if( $shortcut ) return false;
		
		if( $input == 'm' )
		{
			$this->backMain();
			return true;
		}
		if( $input == 'q' )
		{
			$this->backUp();
			return true;
		}
		
		return false;	
	}
	
	public function getTips()
	{
		return "尚未实现";
	}
	
	/**
	 * 处理用户输入消息
	 *
	 * @param any $input
	 * @param boolean $shortcut 短指令
	 * @return unknown
	 */
	public function handle($input,$shortcut = false)
	{
		return "尚未实现";
	}
	
	public function getValue($key)
	{
		if(!isset($this->UUID) || empty($this->UUID))die('UUID Not set!');
		return $this->session->getValue("{$this->UUID}_{$key}");
	}
	public function getStr($key)
	{
		if(!isset($this->UUID) || empty($this->UUID))die('UUID Not set!');
		return $this->session->getStr("{$this->UUID}_{$key}");
	}
	public function set($key,$value)
	{
		if(!isset($this->UUID) || empty($this->UUID))die('UUID Not set!');
		return $this->session->set("{$this->UUID}_{$key}",$value);
	}
	public function query($sql)
	{
		return $this->session->db->fetchQueryAll($sql);
	}
	public function execute($sql)
	{
		return $this->session->db->execute($sql);
	}
	public function getBuddyID()
	{
		return $this->session->buddyID;
	}
	public function getBuddyURI()
	{
		return $this->session->buddyURI;
	}
	public function getBuddyFetion()
	{
		return $this->session->buddyArray['sid'];
	}

	/**
	 * 增加积分
	 *
	 * @param integer 数据库
	 * @param integer 用户ID
	 * @param integer 积分类型，1：每日登录积分 2：游戏积分
	 * @param integer $score  积分
	 * @param string $reason 原因
	 */
	public static function addScore($db,$buddyID,$scoreType,$score,$reason)
	{
		if( $scoreType == 1 )
		{
			$year = date('Y');
			$month = date('m');
			$day = date('d');
			
			$startTime = mktime(0,0,0,$month,$day,$year);
			$endTime = mktime(0,0,0,$month,$day+1,$year);
			
			// 每日积分，首先查看当日有没有积分记录。如果有，则不增加。
			$sql = "select * from fx_scores where buddy_id=$buddyID and score_type=1 and add_time>=$startTime and add_time<$endTime";
			$retArr = $db->fetchQueryAll($sql);
			if( count($retArr) != 0 )return;
		}
		
		/* 增加积分 */
		$time = time(NULL);
		$sql = "insert into fx_scores(buddy_id,score_type,score,reason,add_time) values($buddyID,$scoreType,$score,'$reason',$time)";
		$db->execute($sql);
		
		$sql = "update fx_buddies set score=score+$score where id=$buddyID";
		$db->execute($sql);
	}
}

?>